﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using MM.Common.Base;
using DevExpress.XtraDataLayout;
using DevExpress.XtraEditors;

namespace MM.Common.UI.Controls
{
    public class UCDataLayoutControl : DataLayoutControl, IControlProperty
    {
        // Fields
        private string _ControlCode;
        private string _ControlName;
        private EControlPropertyStatus _ControlProperty;
        private IContainer components;

        // Methods
        public UCDataLayoutControl()
        {
            this.InitializeComponent();
        }

        public UCDataLayoutControl(IContainer container)
        {
            container.Add(this);
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty != EControlPropertyStatus.Write)) && base.Enabled)
            {
                base.Enabled = false;
            }
            this.SetEnabled(base.Enabled);
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty == EControlPropertyStatus.Hide)) && base.Visible)
            {
                base.Visible = false;
            }
        }

        public virtual void SetEnabled(bool enabled)
        {
            for (int i = 0; i < base.Controls.Count; i++)
            {
                if (!(base.Controls[i] is UCLabel))
                {
                    if (base.Controls[i] is BaseEdit)
                    {
                        ((BaseEdit)base.Controls[i]).Properties.ReadOnly = !enabled;
                    }
                    else
                    {
                        base.Controls[i].Enabled = enabled;
                    }
                }
            }
        }

        // Properties
        [Category("MM附加"), Browsable(true), Description("控制本控件对应的系统代码"), DefaultValue((string)null)]
        public string ControlCode
        {
            get
            {
                return this._ControlCode;
            }
            set
            {
                this._ControlCode = value;
            }
        }

        [Browsable(true), Category("MM附加"), Description("控制本控件对应的系统名称"), DefaultValue((string)null)]
        public string ControlName
        {
            get
            {
                return this._ControlName;
            }
            set
            {
                this._ControlName = value;
                if (!base.DesignMode && !string.IsNullOrEmpty(this.ControlCode))
                {
                    if (this._ControlProperty == EControlPropertyStatus.None)
                    {
                        this._ControlProperty = BaseDataHelper.GetFunctionPointProperty(this._ControlCode, this._ControlName);
                    }
                    switch (this._ControlProperty)
                    {
                        case EControlPropertyStatus.Read:
                            base.Enabled = true;
                            base.Visible = true;
                            return;

                        case EControlPropertyStatus.Write:
                            base.Enabled = false;
                            base.Visible = true;
                            return;
                    }
                    base.Visible = false;
                }
            }
        }

        [Browsable(false), DefaultValue(0)]
        public EControlPropertyStatus ControlProperty
        {
            get
            {
                return this._ControlProperty;
            }
        }
    }
}
